package com.centling.smartSealForPhone.activity

import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.centling.smartSealForPhone.R
import com.centling.smartSealForPhone.constants.UrlConstants
import com.centling.smartSealForPhone.utils.OkHttpUtil
import com.centling.smartSealForPhone.utils.ShowToast
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_msg_list.*
import kotlinx.android.synthetic.main.rv_msg_list_item.view.*
import java.text.SimpleDateFormat
import java.util.*

class MessageListActivity : TitleBarActivity() {

    private var pageIndex = 0
    private val reasonList = arrayListOf<MsgBean>()

    private val timeFormat = SimpleDateFormat("yyy-MM-dd HH:mm", Locale.CHINA)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_msg_list)

        setTitleBarText("系统通知")

        rv_msg_list.layoutManager = GridLayoutManager(mContext, 1)
        rv_msg_list.adapter = ApplySealReasonAdapter()

        sendRequestToFetchMsgList(true)
    }

    private fun sendRequestToFetchMsgList(isRefresh: Boolean) {
        if (isRefresh) {
            pageIndex = 0
        }
        getEnqueue(String.format(Locale.CHINA, UrlConstants.FETCH_MSG_LIST, pageIndex), object : OkHttpUtil.NetCallBack {
            override fun onSucceed(json: String?) {
                mProcessDialog.dismiss()

                val reasonListBean = Gson().fromJson(json, MsgListBean::class.java)
                if (reasonListBean.list != null) {
                    reasonList.addAll(reasonListBean.list!!)
                    rv_msg_list.setLoadDataListener { sendRequestToFetchMsgList(false) }
                    if (reasonListBean != null && reasonListBean.list!!.size < 20) {
                        rv_msg_list.loadMoreComplete(true)
                    } else {
                        rv_msg_list.loadMoreComplete(false)
                    }
                } else {
                    rv_msg_list.loadMoreComplete(true)
                }

                rv_msg_list.adapter.notifyDataSetChanged()
                pageIndex++
            }

            override fun onFailed(reason: String?) {
                mProcessDialog.dismiss()
                ShowToast.shortTime(reason)
                rv_msg_list.loadMoreComplete(false)
            }
        })
    }

    inner class ApplySealReasonAdapter : RecyclerView.Adapter<MessageListActivity.MsgListViewHolder>() {
        override fun getItemCount(): Int = reasonList.size

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MsgListViewHolder
                = MsgListViewHolder(LayoutInflater.from(mContext).inflate(R.layout.rv_msg_list_item, parent, false))

        override fun onBindViewHolder(holder: MsgListViewHolder, position: Int) {
            holder.itemView.v_msg_read_indicator.visibility = if(reasonList[position].read == 1) View.VISIBLE else View.GONE
            holder.itemView.tv_msg_title.text = reasonList[position].title
            holder.itemView.tv_msg_time.text = timeFormat.format(Date(reasonList[position].creatTime))
            holder.itemView.tv_msg_digest.text = reasonList[position].messageShort
            holder.itemView.setOnClickListener {
                holder.itemView.v_msg_read_indicator.visibility = View.GONE
                reasonList[position].read = 0
                mContext.startActivity(Intent(mContext, MessageDetailActivity::class.java).putExtra("msg_id", reasonList[position].id))
            }
        }
    }

    inner class MsgListViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView)

    inner class MsgListBean {
        var list: List<MsgBean>? = null
    }

    data class MsgBean(var id: String, val title: String, val creatTime: Long, val messageShort: String, var read: Int)
}
